using UnityEngine;

public class PlatformTemporary : MonoBehaviour
{
	public float fadeTime = 2f;

	private BoxCollider theCollider;

	private Renderer theRenderer;

	private Color finalColor;

	private Color[] originalColors;

	private bool running;

	private bool init = true;

	private float startTime;

	private float myTime = -9999f;

	private void Start()
	{
		theCollider = base.gameObject.GetComponent<BoxCollider>();
		theRenderer = base.gameObject.GetComponent<Renderer>();
		if (Application.isPlaying)
		{
			BallBehaviour.BallResetEvent += OnBallReset;
			EventManager.WorldResetEvent += OnWorldReset;
		}
		finalColor = new Color(0.28f, 0.28f, 0.28f);
		originalColors = new Color[theRenderer.materials.Length];
		for (int i = 0; i < theRenderer.materials.Length; i++)
		{
			originalColors[i] = theRenderer.materials[i].GetColor("_Color");
		}
	}

	private void OnDestroy()
	{
		if (Application.isPlaying)
		{
			BallBehaviour.BallResetEvent -= OnBallReset;
			EventManager.WorldResetEvent -= OnWorldReset;
		}
	}

	private void OnBallReset()
	{
		Reset();
	}

	private void OnWorldReset()
	{
		Reset();
	}

	private void OnCollisionEnter(Collision collision)
	{
		if (!running)
		{
			BallBehaviour instance = BallBehaviour.GetInstance();
			if (instance != null && collision.gameObject == instance.gameObject)
			{
				Activate();
			}
		}
	}

	private void FixedUpdate()
	{
		if (myTime == -9999f)
		{
			myTime = 0f - Time.fixedDeltaTime;
		}
		myTime += Time.fixedDeltaTime;
		if (Game.IsLevelLoading())
		{
			return;
		}
		if (init)
		{
			init = false;
		}
		if (init || !running)
		{
			return;
		}
		float num = myTime - startTime;
		if (num < fadeTime)
		{
			float num2 = 1f - num / fadeTime * 0.75f;
			for (int i = 0; i < originalColors.Length; i++)
			{
				theRenderer.materials[i].SetFloat("_Saturation", num2);
				theRenderer.materials[i].SetColor("_Color", Color.Lerp(finalColor, originalColors[i], num2));
			}
		}
		else
		{
			running = false;
			Disappear();
		}
	}

	public void SetFadeTime(float duration)
	{
		fadeTime = duration;
	}

	public void Activate()
	{
		running = true;
		startTime = myTime;
	}

	public void Disappear()
	{
		theCollider.enabled = false;
		theRenderer.enabled = false;
	}

	public void Reset()
	{
		running = false;
		theCollider.enabled = true;
		theRenderer.enabled = true;
		for (int i = 0; i < originalColors.Length; i++)
		{
			theRenderer.materials[i].SetFloat("_Saturation", 1f);
			theRenderer.materials[i].SetColor("_Color", originalColors[i]);
		}
	}
}
